package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.SpecKeyDTO;
import io.github.talelin.latticy.model.SpecKeyDO;
import io.github.talelin.latticy.mapper.SpecKeyMapper;
import io.github.talelin.latticy.service.SpecKeyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@TaleLin
 * @since 2021-06-19
 */
@Service
public class SpecKeyServiceImpl extends ServiceImpl<SpecKeyMapper, SpecKeyDO> implements SpecKeyService {

    @Override
    public List<SpecKeyDO> getBySpuId(Long spuId) {
        return this.baseMapper.getBySpuId(spuId);
    }

    @Override
    public void saveSpecKey(SpecKeyDTO specKeyDTO) {
        SpecKeyDO specKeyDO = new SpecKeyDO();
        BeanUtils.copyProperties(specKeyDTO, specKeyDO);

        this.save(specKeyDO);
    }

    @Override
    public void update(Long id, SpecKeyDTO specKeyDTO) {
        SpecKeyDO specKeyDO = this.getById(id);
        if (specKeyDO == null) {
            throw new NotFoundException(10000);
        }
        BeanUtils.copyProperties(specKeyDTO, specKeyDO);
        this.updateById(specKeyDO);
    }

    @Override
    public void deleteSpecKey(Long id) {
        SpecKeyDO specKeyDO = this.getById(id);
        if (specKeyDO == null) {
            throw new NotFoundException(10000);
        }
        this.deleteSpecKey(id);
    }

    @Override
    public IPage<SpecKeyDO> getPage(Long count, Long page) {

        Page<SpecKeyDO> pager = new Page<>(page, count);
        return this.page(pager);
    }

}
